package app.project.streetlensproject.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import app.project.streetlensproject.HomeActivity;
import app.project.streetlensproject.R;
import app.project.streetlensproject.serverapi.ServerAPI;
import app.project.streetlensproject.util.AsyncTask;
import app.project.streetlensproject.util.CommonUtils;
import app.project.streetlensproject.util.Constants;
import app.project.streetlensproject.util.GetDataTask;
import app.project.streetlensproject.util.IGetDataTask;
import app.project.streetlensproject.util.StringUtils;

public class FgResetPass2 extends Fragment implements OnClickListener {
	private Button btnOk, btnCancel;
	private EditText edtEmail, edtCode;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fg_reset_pass2, container, false);
		btnOk = (Button) v.findViewById(R.id.btn_ok);
		btnCancel = (Button) v.findViewById(R.id.btn_cancel);
		edtEmail = (EditText) v.findViewById(R.id.edt_email);
		edtCode = (EditText) v.findViewById(R.id.edt_code);

		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		String email = CommonUtils.getStringSetting(getActivity(), Constants.EMAIL, null);
		if (email != null)
			edtEmail.setText(email);
		return v;
	}

	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_ok) {
			final String email = edtEmail.getText().toString();
			final String code = edtCode.getText().toString().trim();
			if (!StringUtils.vaildEmail(email)) {
				Toast.makeText(getActivity(), R.string.email_not_vaild, Toast.LENGTH_LONG).show();
				return;
			}
			new GetDataTask(getActivity(), new IGetDataTask() {
				@Override
				public void onPostExecute(boolean result) throws Exception {
					super.onPostExecute(result);
					if (result) {
						((HomeActivity) getActivity()).gotoCategory();
						Toast.makeText(getActivity(), R.string.send_pass_sucess, Toast.LENGTH_LONG).show();
					} else
						Toast.makeText(getActivity(), R.string.has_error, Toast.LENGTH_LONG).show();
				}

				@Override
				public boolean doInBackGround() throws Exception {
					return ServerAPI.ResetPassword(email, code);
				}
			}, true).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else if (id == R.id.btn_cancel) {
			((HomeActivity) getActivity()).gotoCategory();
		}
	}
}